package com.astrob.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;

import com.astrob.NavFrameSDK;
import com.astrob.NavFrameSDK.NavPosition;
import com.astrob.model.LonLat;
import com.astrob.naviframe.Start;
import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.astrob.util.Utils;
import com.nhc.model.MapSetInfo;

import org.mapsforge.core.util.MercatorProjection;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.model.DisplayModel;
import org.osmdroid.util.TileSystem;
import org.osmdroid.views.MapView;
import org.osmdroid.views.Projection;


public class Car {

    public static final int BOTTOMCENTER = 0; //车头朝前
    public static final int CENTER = 1; //地图指北
    private int mCarposMode = BOTTOMCENTER;

    private boolean mIs3D = false;
    private boolean mIsFreeMoving = true;

    // 匹配后的位置
    public NavPosition navpos_ = NavFrameSDK.getInstance().new NavPosition();
    // 原始Map对应的坐标
    private Point mCarPos = new Point();

    public void setCarNavPos(NavPosition navpos) {
        navpos_ = navpos;
    }

    public void setCarHeading(float degree) {
//        degree = 50; //测试
        navpos_.dir = degree;
    }

    public NavPosition getCarNavPos() {
        return navpos_;
    }

    public LonLat getCarLL() {
        return new LonLat(navpos_.pos.lon, navpos_.pos.lat);
    }

    public void set3D(boolean is3D) {
        mIs3D = is3D;
        mCarposMode = BOTTOMCENTER;
    }

    public void setCarposMode(int carposMode) {
        mCarposMode = carposMode;
    }

    public int getCarposMode() {
        return mCarposMode;
    }

    public void setFreeMoving(boolean isFreeMoving) {
        mIsFreeMoving = isFreeMoving;
        if (!mIsFreeMoving) {
            setCarposMode(mCarposMode);
        }
    }

    public boolean isFreeMoving() {
        return mIsFreeMoving;
    }

    private int mCarId = R.drawable.i3_car00;

    private void setCarIcon() {
//        if (!mIsFreeMoving && mCarposMode == BOTTOMCENTER) {
//            if (mIs3D) {
//                mCarId = R.drawable.i3_car3d;
//            } else {
//                mCarId = R.drawable.i3_car00;
//            }
//            return;
//        }
//
//        int nIndex = (int) (navpos_.dir / 11.25 + 0.5f);
//        if (nIndex > 31)
//            nIndex = 0;
//
//        mCarId = R.drawable.i3_car00 + nIndex;

        if (MapSetInfo.getInstance().getCaricontype() == 0) {
            mCarId = R.drawable.i3_car00;
        } else if (MapSetInfo.getInstance().getCaricontype() == 1) {
            mCarId = R.drawable.i3_car3d;
        }
    }

    protected void drawCarRange(Canvas canvas, int x, int y, int range) {
        Paint paint = new Paint();
        paint.setColor(0x263b90df);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawCircle(x, y, range, paint);

        Paint paint1 = new Paint();
        paint1.setColor(0x803474da);
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setStrokeWidth((float) 2.0);
        paint1.setAntiAlias(true);
        canvas.drawCircle(x, y, range, paint1);
    }

    public void draw(Context context, Canvas canvas, float sx, float sy) {
        setCarIcon();
        getCarPos();

        int signal = navpos_.signal;

	/*	Bitmap bm = ((BitmapDrawable) context.getResources()
                .getDrawable(mCarId)).getBitmap();*/
        // 指北针
        Bitmap bmBk = ((BitmapDrawable) context.getResources()
                .getDrawable(R.drawable.shapan)).getBitmap();

        // 车标
        Bitmap bm = ((BitmapDrawable) context.getResources()
                .getDrawable(mCarId)).getBitmap();

        if (mIsFreeMoving || mCarposMode == CENTER) {
            bm = Start.rotateBitmap(bm, (int) navpos_.dir);
        } else {
            bmBk = Start.rotateBitmap(bmBk, -(int) navpos_.dir);
        }

        int x = (int) (mCarPos.x * sx - bmBk.getWidth() / 2);
        int y = (int) (mCarPos.y * sy - bmBk.getHeight() / 2);

        if (!mIsFreeMoving) {
//            canvas.drawBitmap(bmBk, x, y, null);
        }

        x = (int) (mCarPos.x * sx - bm.getWidth() / 2);
        y = (int) (mCarPos.y * sy - bm.getHeight() / 2);

        if (signal == 100 || navpos_.vel == 2000) {
            bm = grey(bm);
        }
        canvas.drawBitmap(bm, x, y, null);

        if (signal == 100 || navpos_.vel == 2000) {
            return;
        }

        if (signal > 50 || navpos_.vel == 2000) {
            signal = 100;
            return;
        } else if (signal < 10) {
            signal = 10;
        }
        int range = (int) (Start.getInstance().MapKit().meterDis2Pix(signal, mapWidth_) * sx);
        if (range >= 10) {
            drawCarRange(canvas, (int) (mCarPos.x * sx),
                    (int) (mCarPos.y * sy), range);
        }
    }

    public static final Bitmap grey(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Bitmap faceIconGreyBitmap = Bitmap
                .createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(faceIconGreyBitmap);
        Paint paint = new Paint();
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0);
        ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(
                colorMatrix);
        paint.setColorFilter(colorMatrixFilter);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        return faceIconGreyBitmap;
    }


    private int mapWidth_ = 0;
    private int mapHeight_ = 0;

    public void setMapsize(int mapWidth, int mapHeight) {
        mapWidth_ = mapWidth;
        mapHeight_ = mapHeight;
    }

    public Point getCarPos() {
        if (mIsFreeMoving) {
            LonLat carll = new LonLat(navpos_.pos.lon, navpos_.pos.lat);
            carll = Utils.wgs84togcj02(carll.GetLon(), carll.GetLat());//Utils GPS to 火星坐标
            mCarPos = Start.getInstance().MapKit().LL2XY(carll);
        } else {
            mCarPos.x = (mapWidth_ / 2);
            mCarPos.y = (mapHeight_ / 2);

            switch (mCarposMode) {
                default:
                case BOTTOMCENTER:
                    if (SysUtils.getInstance().mapsource != 1000) {
                        mCarPos.y = (int) (mapHeight_ * 0.5);
                    } else {
                        mCarPos.y = (int) (mapHeight_ * _bc);
                    }

                    break;
                case CENTER:
                    break;
            }
        }
        return mCarPos;
    }

    float _bc = 0.75f;

    //设置，车头朝前模式时，车在屏幕的Y轴位置
    public void setCQY(float s) {
        _bc = s;
    }


    Point LL2XY(LonLat ll, MapView mapView) {
        Point A = new Point();
        Point B = new Point();
        mapView.getProjection().toProjectedPixels(ll.GetLat(),
                ll.GetLon(), A);
        Projection pj = mapView.getProjection();
        pj.toPixelsFromProjected(A, B);
        return B;
    }

    public void draw2(Context context, Canvas canvas, MapView mapView, boolean isgcj02) {
        if (mapView == null) {
            return;
        }

        setCarIcon();

        int w = mapView.getWidth();
        int h = mapView.getHeight();

        LonLat carll = new LonLat(navpos_.pos.lon, navpos_.pos.lat);
        if (isgcj02) {
            carll = Utils.wgs84togcj02(carll.GetLon(), carll.GetLat());//Utils GPS to 火星坐标
        }

        if (mIsFreeMoving) {
            mCarPos = LL2XY(carll, mapView);
        } else {
            mCarPos.x = (w / 2);
            mCarPos.y = (h / 2);

            switch (mCarposMode) {
                default:
                case BOTTOMCENTER:
                    mCarPos.y = (int) (h * _bc);
                    break;
                case CENTER:
                    break;
            }
        }

        int signal = navpos_.signal;

        // 指北针
        Bitmap bmBk = ((BitmapDrawable) context.getResources()
                .getDrawable(R.drawable.shapan)).getBitmap();

        // 车标
        Bitmap bm = ((BitmapDrawable) context.getResources()
                .getDrawable(mCarId)).getBitmap();

        if (mIsFreeMoving || mCarposMode == CENTER) {
            bm = Start.rotateBitmap(bm, (int) navpos_.dir);
        } else {
//            bmBk = Start.rotateBitmap(bmBk, -(int) navpos_.dir);
            bm = Start.rotateBitmap(bm, (int) navpos_.dir);
        }

        int x = (int) (mCarPos.x - bmBk.getWidth() / 2);
        int y = (int) (mCarPos.y - bmBk.getHeight() / 2);

        if (!mIsFreeMoving) {
//            canvas.drawBitmap(bmBk, x, y, null);
        }

        x = (int) (mCarPos.x - bm.getWidth() / 2);
        y = (int) (mCarPos.y - bm.getHeight() / 2);

        if (signal == 100 || navpos_.vel == 2000) {
            bm = grey(bm);
        }
        canvas.drawBitmap(bm, x, y, null);

        if (signal == 100 || navpos_.vel == 2000) {
            return;
        }

        if (signal > 50 || navpos_.vel == 2000) {
            signal = 100;
            return;
        } else if (signal < 10) {
            signal = 10;
        }

        float range = signal / (float) TileSystem.GroundResolution(carll.GetLat(), mapView.getZoomLevel());

        if (range >= 10) {
            drawCarRange(canvas, mCarPos.x, mCarPos.y, (int) range);
        }
    }

    public void draw3(Context context, org.mapsforge.core.graphics.Canvas canvas, org.mapsforge.core.model.BoundingBox bb, byte zoomLevel, org.mapsforge.core.model.Point topLeftPoint, DisplayModel displayModel) {

        setCarIcon();

        android.graphics.Canvas androidCanvas = AndroidGraphicFactory.getCanvas(canvas);

        int w = canvas.getWidth();
        int h = canvas.getHeight();

        LonLat carll = new LonLat(navpos_.pos.lon, navpos_.pos.lat);

        if (mIsFreeMoving) {
            long mapSize = MercatorProjection.getMapSize(zoomLevel, displayModel.getTileSize());
            int x = (int)(MercatorProjection.longitudeToPixelX(carll.GetLon(), mapSize) - topLeftPoint.x);
            int y = (int)(MercatorProjection.latitudeToPixelY(carll.GetLat(), mapSize) - topLeftPoint.y);
            mCarPos = new Point(x, y);
        } else {
            mCarPos.x = (w / 2);
            mCarPos.y = (h / 2);

            switch (mCarposMode) {
                default:
                case BOTTOMCENTER:
                    mCarPos.y = (int) (h * _bc);
                    break;
                case CENTER:
                    break;
            }
        }

        int signal = navpos_.signal;

        // 指北针
        Bitmap bmBk = ((BitmapDrawable) context.getResources()
                .getDrawable(R.drawable.shapan)).getBitmap();

        // 车标
        Bitmap bm = ((BitmapDrawable) context.getResources()
                .getDrawable(mCarId)).getBitmap();



        if (mIsFreeMoving || mCarposMode == CENTER) {
            bm = Start.rotateBitmap(bm, (int) navpos_.dir);
        } else {
//            bmBk = Start.rotateBitmap(bmBk, -(int) navpos_.dir);
            bm = Start.rotateBitmap(bm, (int) navpos_.dir);
        }

        int x = (int) (mCarPos.x - bmBk.getWidth() / 2);
        int y = (int) (mCarPos.y - bmBk.getHeight() / 2);

        if (!mIsFreeMoving) {
//            canvas.drawBitmap(bmBk, x, y, null);
        }

        x = (int) (mCarPos.x - bm.getWidth() / 2);
        y = (int) (mCarPos.y - bm.getHeight() / 2);

        if (signal == 100 || navpos_.vel == 2000) {
            bm = grey(bm);
        }
        androidCanvas.drawBitmap(bm, x, y, null);

        if (signal == 100 || navpos_.vel == 2000) {
            return;
        }

        if (signal > 50 || navpos_.vel == 2000) {
            signal = 100;
            return;
        } else if (signal < 10) {
            signal = 10;
        }

        float range = signal / (float) TileSystem.GroundResolution(carll.GetLat(), zoomLevel);

        if (range >= 10) {
            drawCarRange(androidCanvas, mCarPos.x, mCarPos.y, (int) range);
        }
    }

}
